package com.ophelisis.actions.common;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.ophelisis.persistence.dao.ZipcodeDAO;
import com.ophelisis.persistence.domain.ZipcodeVO;

/** <pre>
 * Class : com.apexcns.xdp.actions.common.Zipcode / Zipcode.java
 * 	Define Class Object : 
 * <br>
 * </pre>
 * @author hlee / apexcns,Inc.
 * @version 1.0
 * @see javadoc
 * @since 2013. 01. 17.
 */
@Controller
public class Zipcode {
	@Autowired
	ZipcodeDAO zipcodeDAO;
	
	private List<ZipcodeVO> zipcodeList;
	
	private Log log = LogFactory.getLog(this.getClass());
	private static final String _LogPrefixStr = "Zipcode Log> ";

	@RequestMapping(value="zipcode.action")
	public ModelAndView zipcode(
			HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam(value="target", defaultValue="zcMain") String sTarget,
			@RequestParam(value="_startRowNo", defaultValue="0") String sStartRowNo,
			@RequestParam(value="_pageSize", defaultValue="15") String sPageSize,
			@RequestParam(value="searchVal1", defaultValue="") String sSearchVal1) throws Exception {
		ModelAndView mav = new ModelAndView();
		
		log.debug(_LogPrefixStr + "|" + "------------------------------------------------------------------------------------");
		log.debug(_LogPrefixStr + "|" + " Zipcode called !!!");
		log.debug(_LogPrefixStr + "|" + "------------------------------------------------------------------------------------");
		
		try {
			// 공통 요소
			mav.addObject("_startRowNo", sStartRowNo);
			mav.addObject("_pageSize", sPageSize);
			mav.addObject("searchVal1", sSearchVal1);
			
			Map<String, Object> params = new HashMap<String, Object>();
			
			if (sTarget.equals("zcMain")) {
				mav = returnMgmt("main", mav, params);
			} else if (sTarget.equals("zcList")) {
				int iStartRowNo = Integer.parseInt(sStartRowNo); // 해당 페이지의 시작 번호
				int iPageSize = Integer.parseInt(sPageSize); // 한 페이지에 보일 레코드 수
				
				params.put("startRowNo", iStartRowNo);
				params.put("pageSize", iPageSize);
				params.put("searchVal1", sSearchVal1);
	
				mav = returnMgmt("list", mav, params);
			}
		} catch(Exception e) {
			e.printStackTrace();
		}
		
		return mav;
	}
	
	public ModelAndView returnMgmt(
			@RequestParam(value="cmd", defaultValue="list") String cmd,
			@ModelAttribute ModelAndView mav,
			Map<String, Object> params) throws Exception {
		if (cmd.equals("main")) {
			mav.setViewName("/WEB-INF/jsp/common/zipcode.jsp");
			
			log.debug(_LogPrefixStr + "|" + "------------------------------------------------------------------------------------");
			log.debug(_LogPrefixStr + "|" + " Zipcode Main-Page called !!!");
			log.debug(_LogPrefixStr + "|" + "------------------------------------------------------------------------------------");
		} else if (cmd.equals("list")) {
			int iTotalCount = 0;
			
			try {
				iTotalCount = zipcodeDAO.getZipcodeListCnt(params); // 쿼리 결과 카운트
				zipcodeList = zipcodeDAO.getZipcodeList(params); // 쿼리 결과
			} catch (Exception e) {
				log.error("DB 정보 획득 failed!", e);
			}

			mav.addObject("totalCnt", String.valueOf(iTotalCount));
			mav.addObject("zipcodeList", zipcodeList);
			mav.setViewName("/WEB-INF/jsp/common/zipcodeList.jsp");
			
			log.debug(_LogPrefixStr + "|" + "------------------------------------------------------------------------------------");
			log.debug(_LogPrefixStr + "|" + " Zipcode List-Page called !!!");
			log.debug(_LogPrefixStr + "|" + "------------------------------------------------------------------------------------");
		}
		
		return mav;
	}
}
